package com.bazinga.contact.view;

import java.sql.Time;
import java.util.Map;

import com.bazinga.contact.controller.JobDetailsManager;
import com.bazinga.contact.controller.JobScheduleManager;
import com.bazinga.contact.model.JobDetails;
import com.bazinga.contact.model.JobSchedule;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;


public class CreateJobAction extends ActionSupport{

	private static final long serialVersionUID = 9149826260758390091L;
	private JobDetails jobDetails;
	private JobSchedule jobSchedule;
	private Long id;
	private String[] daysofweek;
	private Map<String, Object> session;
	private String startTime;
	private String endTime;

	private JobDetailsManager linkController;
	private JobScheduleManager jsLinkController;
	
	public JobDetails getJobDetails() {
		return jobDetails;
	}

	public void setJobDetails(JobDetails jobDetails) {
		this.jobDetails = jobDetails;
	}

	public CreateJobAction() {
		linkController = new JobDetailsManager();
		jsLinkController = new JobScheduleManager();
	}

	public String execute() {
		return SUCCESS;
	}

	public String add() {
		
		this.session = ActionContext.getContext().getSession();
		if(session.containsKey("userid")) {
			jobDetails.setUserid( Long.parseLong((String)session.get("userid")));
		}
		try {
		   jobDetails = linkController.add(getJobDetails());
		   jobSchedule = new JobSchedule();
		   jobSchedule.setJobid(jobDetails.getJobid());
		   jobSchedule.setStart_time(Time.valueOf(startTime));
		   jobSchedule.setEnd_time(Time.valueOf(endTime));
		   for (String dayofweek : daysofweek){
			   jobSchedule.setDayofweek(dayofweek);
			   jsLinkController.add(jobSchedule);
		   }
		} catch (Exception e) {
			e.printStackTrace();
		}
		return SUCCESS;
	}

	public String delete() {
		linkController.delete(getId());
		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public JobSchedule getJobSchedule() {
		return jobSchedule;
	}

	public void setJobSchedule(JobSchedule jobSchedule) {
		this.jobSchedule = jobSchedule;
	}

	public String[] getDaysofweek() {
		return daysofweek;
	}

	public void setDaysofweek(String[] daysofweek) {
		this.daysofweek = daysofweek;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	
}
